package com.lqs.blog.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lqs.blog.mapper.PermissionMapper;
import com.lqs.blog.pojo.Permission;
import com.lqs.blog.vo.PageResult;
import com.lqs.blog.vo.Result;
import com.lqs.blog.vo.params.PageParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class PermissionService {
    @Autowired
    private PermissionMapper permissionMapper;

    public Result permission(PageParam pageParam) {
        Page<Permission> page = new Page<>(pageParam.getCurrentPage(), pageParam.getPageSize());
        LambdaQueryWrapper<Permission> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(pageParam.getQueryString())) {//判断查询条件是否为空
            wrapper.eq(Permission::getName, pageParam.getQueryString());
        }
        Page<Permission> permissionPage = permissionMapper.selectPage(page, wrapper);
//        System.out.println(permissionPage.getTotal() + "\n" + permissionPage.getRecords());
//        long total = permissionPage.getTotal();
////        System.out.println("total:" + total);

        PageResult<Permission> pageResult = new PageResult<>();
        pageResult.setTotal(permissionPage.getTotal());
        pageResult.setList(permissionPage.getRecords());
//        System.out.println(Result.success(pageResult));
        return Result.success(pageResult);
    }

    public Result add(Permission permission) {
        permissionMapper.insert(permission);
        return Result.success(null);
    }

    public Result update(Permission permission) {
        permissionMapper.updateById(permission);
        return Result.success(null);
    }

    public Result delete(long id) {
        permissionMapper.deleteById(id);
        return Result.success(null);
    }
}
